const express=require('express')
const QRCode=require('qrcode')
const session=require('express-session')
const app=express();
const PORT=9000;
app.use(session({
    secret:"your secret key",
    resave:false,
    saveUninitialized:true,
    cookie:{maxAge:6000}
}))
app.get('/generate-qrcode',(req,res)=>{
 const token=Math.random().toString(36).substring(7);
   req.session.qrcodetoken=token;
   req.session.qrcodeExpiry=new Date()+30000;
   QRCode.toDataURL(token,(err,url)=>{
    if(err) throw err;
    res.send(url)
   })
})
// 验证二维码的路由  
app.get('/verify-qrcode', (req, res) => {  
  const token = req.query.token;  
  const currentTime = Date.now();  
  if (req.session.qrcodeToken === token && req.session.qrcodeExpiry > currentTime) {  
    // 验证通过，可以重定向到前端路由或百度  
    res.send({ redirectUrl: 'http://yourfrontend.com/' }); // 或者 'https://www.baidu.com/'  
  } else {  
    // 验证失败或过期  
    res.status(400).send('Invalid or expired QR code');  
  }  
});  
app.listen(PORT, () => {  
  console.log(`Server is running on port ${PORT}`);  
});
